package com.yinp.proapp.module.customview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.yinp.proapp.R;

/**
 * @author yinp.
 * @title
 * @description
 * @date 2019/9/12,13:54.
 */

public class SpiderWebView extends View {

    public SpiderWebView(Context context) {
        this(context, null);
    }

    public SpiderWebView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SpiderWebView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Paint paint;
    private Paint paint1;

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);

        paint1 = new Paint();
        paint1.setStyle(Paint.Style.FILL);
        paint1.setColor(ContextCompat.getColor(getContext(), R.color.blue_start));
    }

    private float radius;
    private int centerX;
    private int centerY;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        radius = Math.min(w, h) / 2;
        centerX = w / 2;
        centerY = h / 2;
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPolygon(canvas);
        drawLines(canvas);
        drawArea(canvas);
    }

    private static final double ANGLE = 2 * Math.PI / 360 * 60;//60度角，直接60是不行的

    private void drawPolygon(Canvas canvas) {
        Path path = new Path();
        float r = radius / 6;
        for (int i = 1; i <= 6; i++) {
            float curR = r * i;
            path.reset();
            for (int j = 0; j < 6; j++) {
                if (j == 0) {
                    path.moveTo(centerX + curR, centerY);
                } else {
                    float x = (float) (centerX + curR * Math.cos(ANGLE * j));
                    float y = (float) (centerY + curR * Math.sin(ANGLE * j));
                    path.lineTo(x, y);
                }
            }
            path.close();
            canvas.drawPath(path, paint);
        }
    }

    private void drawLines(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < 6; i++) {
            path.reset();
            path.moveTo(centerX, centerY);
            float x = (float) (centerX + radius * Math.cos(ANGLE * i));
            float y = (float) (centerY + radius * Math.sin(ANGLE * i));
            path.lineTo(x, y);
            canvas.drawPath(path, paint);
        }
    }

    private int[] dian = {2, 5, 3, 1, 4, 6};
    private float maxValue = 6;

    private void drawArea(Canvas canvas) {
        Path path = new Path();
        paint1.setAlpha(127);
        for (int i = 0; i < dian.length; i++) {
            double percent = dian[i] / maxValue;
            float x = (float) (centerX + radius * Math.cos(ANGLE * i) * percent);
            float y = (float) (centerY + radius * Math.sin(ANGLE * i) * percent);
            if (i == 0) {
                path.moveTo(x, centerY);
            } else {
                path.lineTo(x, y);
            }
            canvas.drawCircle(x, y, 6, paint1);
        }
        path.close();
        canvas.drawPath(path, paint1);
    }
}
